<template>
  <div class="evaluatuion-container">
    <userHeader/>
    <div class="breadClum">
      <span>
          <router-link :to="{path:'/pageIndex'}">   <a class="bread1">当前位置：首页  >  </a></router-link>
          <router-link :to="{path:'/comment'}">  <a class="bread2">内控评价 ></a></router-link>
          <a class="bread2">内控定量评价 </a>
      </span>
    </div>
    <div class="title" v-if="evaplan_type_id!=''"><span>评价单位: {{company_name}}</span><span class="evaluatuion-time">评价期间: {{duringEvaluation}}</span>
    </div>
    <div class="title" v-if="evaplan_type_id ==''">请先选择评价类型</div>
    <div class="evaluatuion-content">
      <div class="navList" style="margin-bottom: 20px;">
        <el-button type="primary" size="small" @click="editQuantitative">编辑</el-button>
        <el-button type="primary" size="small" @click="saveQuantitative" style="margin-right:12px;">保存</el-button>
        <el-dropdown trigger="click" style="cursor:pointer;background: #f0b914;padding-top:6px;padding-bottom: 8px;"
                     @command="selectQuantitative">
          <span class="el-dropdown-link">
            {{qualifiedListSelect}}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown" style="padding: 0 20px;">
            <el-dropdown-item v-for="(item,index) in qualifiedList" :key="index" :command="item">
              {{item.evaplan_type_name}}
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <div class="eval-content" v-if="evaplan_type_id!=''">
        <!--内控过程评分-->
        <div class="process-score inner-container">
          <h3 class="inner-title">一、内控过程评分</h3>
          <el-table
                  width="100%"
                  ref="processScore"
                  :data="control_internal"
                  stripe
                  show-summary
                  :summary-method="summaryProcess"
                  sum-text="内控过程合计"
          >
            <el-table-column
                    label="编号"
                    type="index"
                    width="70">
            </el-table-column>
            <el-table-column
                    prop="process_name"
                    label="流程名称"
                    width="600">
            </el-table-column>
            <el-table-column
                    prop="standard_score"
                    label="标准分值"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="evaluation_score"
                    label="评价分值"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="examine_score"
                    label="评价得分"
                    width="100">
            </el-table-column>
            <el-table-column>
            </el-table-column>
          </el-table>
        </div>
        <!--内控指标评分-->
        <div class="quota-score inner-container" v-if="bl_year_evaluate==1">
          <h3 class="inner-title">二、内控指标评分</h3>
          <el-form>
            <el-table
                    ref="quotaScore"
                    :data="evaluation_index"
                    stripe
                    width="100%"
            >
              <el-table-column
                      label="编号"
                      type="index"
              >
              </el-table-column>
              <el-table-column
                      prop="inin_title"
                      label="评价指标"
                      width="350">
              </el-table-column>
              <el-table-column
                      prop="inin_formula"
                      label="计算公式"
                      width="438">
              </el-table-column>
              <el-table-column
                      prop="inin_type"
                      label="指标类型"
                      width="100">
                <template slot-scope="scope">
                  <span>{{scope.row.inin_type == "1" ? "正指标":"负指标"}}</span>
                </template>
              </el-table-column>
              <el-table-column
                      label="本期目标"
                      width="100">
                <template slot-scope="scope">
                  <el-form-item style="margin-bottom:0;">
                    <el-input v-if="is_edit" type="text" v-model="scope.row.current_objectives" placeholder="请输入本期目标"></el-input>
                    <span v-else>{{scope.row.current_implementation}}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      label="本期实际"
                      width="100">
                <template slot-scope="scope">
                  <el-form-item style="margin-bottom:0;">
                    <el-input v-if="is_edit" type="text" v-model="scope.row.current_implementation" placeholder="请输入本期实际"></el-input>
                    <span v-else>{{scope.row.current_implementation}}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      prop="previous_implementation"
                      label="上期实际"
                      width="100">
                <template slot-scope="scope">
                  <el-form-item style="margin-bottom:0;">
                    <el-input v-if="is_edit" type="text" v-model="scope.row.previous_implementation" placeholder="请输入上期实际"></el-input>
                    <span v-else>{{scope.row.previous_implementation}}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      label="增加幅度">
                <el-table-column
                        prop="objective_compare"
                        label="目标比较"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="previous_compare"
                        label="上期比较"
                        width="100">
                </el-table-column>
              </el-table-column>
              <el-table-column
                      label="评分">
                <el-table-column
                        prop="inin_standard_score"
                        label="标准分值"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="coinso_score"
                        label="评价分值"
                        width="100">
                </el-table-column>
              </el-table-column>
              <el-table-column>

              </el-table-column>
            </el-table>
          </el-form>
        </div>
        <!--调整因素评分-->
        <div class="factor-score  inner-container" v-if="bl_year_evaluate==1">
          <h3 class="inner-title">三、调整因素评分</h3>
          <el-form>
            <el-table
                    ref="factorScore"
                    :data="adjustment_factor"
                    width="100%">
              <el-table-column
                      prop="element_name"
                      label="编号"
                      type="index"
                      width="125">
              </el-table-column>
              <el-table-column
                      prop="adpa_title"
                      label="调整事项"
                      width="600">
              </el-table-column>
              <el-table-column
                      label="标识"
                      width="100">
                <template slot-scope="scope">
                  <!--prop="adpa_identify"-->
                  <p class="walk_through">
                  <span @click.stop="toggleAdpaIdentify(scope.$index,1)">
                     <b :class="{'e-wrong':true,'e-wrong-active': scope.row.adpa_identify === '0' }"></b>
                    <!--<b class="e-wrong-disActive e-wrong " v-else></b>-->
                  </span>
                    <span @click.stop="toggleAdpaIdentify(scope.$index,2)">
                    <b :class="{'e-right-active':scope.row.adpa_identify === '1', 'e-right':true}" ></b>
                  </span>
                  </p>
                </template>
              </el-table-column>
              <el-table-column
                      label="调整事项描述"
                      width="800">
                <template slot-scope="scope">
                  <el-form-item style="margin-bottom: 0;">
                    <el-input v-if="is_edit" type="textarea" :row="1" v-model="scope.row.coadpaso_content"></el-input>
                    <p v-else>{{scope.row.coadpaso_content}}</p>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      label="评价得分"
                      width="100">
                <template slot-scope="scope">
                  <el-form-item style="margin-bottom: 0;">
                    <el-input v-if="is_edit" type="input" v-model="scope.row.coadpaso_score"></el-input>
                    <p v-else>{{scope.row.coadpaso_score}}</p>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column>

              </el-table-column>
            </el-table>
          </el-form>
        </div>

        <!--结论-->
        <div class="evaluatuion-results">
          <p>评价结果:{{evaluate_results}}</p>
        </div>
      </div>

    </div>



  </div>
</template>
<script>
  import userHeader from '@/components/header.vue'
  import {formatTimeIncludeText} from '@/utils/index.js'

  export default ({
    data() {
      return {
        is_edit:false,
        qualifiedList: [],
        qualifiedListSelect: '评价类型',
        company_name: '',
        evaplan_type_id: '',
        duringEvaluation: '',
        control_internal: [], //内控过程评分
        evaluation_index: [], //内控指标评分
        adjustment_factor: [], //调整因素评分
        bl_year_evaluate:"",
        evaluate_results: '',
        internal_amount_score:'',
        total_evaluation_score: '',
        total_standard_score: ''
      }
    },
    components: {
      userHeader,
    },
    created() {
      this.getQualifiedList().then(res => {
        console.log(res)
        if (res.data.code == 0) {
          this.qualifiedList = res.data.data
        }
      })
    },
    computed: {
      TimeFormat() {
        var temptime = this.startTime + '000';
        return formatTimeIncludeText(temptime)
      }
    },
    methods: {
      summaryProcess(param){
        console.log(param)
        const {
          columns,
          data
        } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '内控过程合计';
            return;
          }
          switch (column.property) {
            // case "process_name":
            //   sums[index] = this.commentTableData.process_total;
            //   break;
            case "process_name":
              sums[index] = '';
              break;
            case "standard_score":
              sums[index] = this.total_standard_score;
              break;
            case "evaluation_score":
              sums[index] = this.total_evaluation_score;
              break;
            case "examine_score":
              sums[index] = this.internal_amount_score;
              break;
            default:
              sums[index] = "--";
              break;
          }
        });
        return sums;
      },
      toggleAdpaIdentify(index,flag){
        if(!this.is_edit){
          this.$alert('编辑操作请先点击编辑按钮！',"提示")
          return false
        }
        this.adjustment_factor.forEach((el,ind)=>{
          if(index == ind ){
            if(flag==1){
              el.adpa_identify = "0";
              el.coadpaso_score = 0;
              el.coadpaso_content = '';

            }else{
              el.adpa_identify = "1"
            }
          }
        })
        // if(this.adjustment_factor[index].)
        console.log(index)

      },
      selectQuantitative(command) {
        this.evaplan_type_id = '';
        this.qualifiedListSelect = command.evaplan_type_name;
        // /api/v1/flow/evaluationquantitative/:evaplan_type_id
        this.axios.get('/api/v1/flow/evaluationquantitative/' + command.evaplan_type_id).then(res => {
          console.log(res.data)
          if(res.data.code==0){
            //此处绑定了 页面头部的 基础信息 在数据正确返回时赋值
            this.evaplan_type_id = command.evaplan_type_id;


            this.startTime = res.data.data.start_time;
            this.endTime = res.data.data.end_time
            this.company_name = res.data.data.company_name;
            // control_internal:[], //内控过程评分
            //   evaluation_index:[], //内控指标评分
            //   adjustment_factor:[] //调整因素评分

            this.control_internal = res.data.data.control_internal;
            this.evaluation_index = res.data.data.evaluation_index;
            this.adjustment_factor = res.data.data.adjustment_factor;
            this.evaluate_results = res.data.data.evaluate_results;
            this.bl_year_evaluate = res.data.data.bl_year_evaluate;
            this.internal_amount_score = res.data.data.internal_amount_score;

            this.total_evaluation_score = res.data.data.total_evaluation_score;
            this.total_standard_score = res.data.data.total_standard_score;
          }else {
            this.$alert(res.data.message,"提示")
          }
        }).then(() => {
          var start = formatTimeIncludeText(this.startTime + '000')
          var end = formatTimeIncludeText(this.endTime + '000')
          this.duringEvaluation = start + "-" + end
        })

      },
      saveQuantitative() {
        if(this.bl_year_evaluate==0){
          this.axios.put('api/v1/flow/evaluationquantitative/'+this.evaplan_type_id,{
            evaplan_type_id:this.evaplan_type_id
          }).then(res=>{
            if(res.data.code==0){
              // 重新请求当前评价计划的数据
              this.axios.get('/api/v1/flow/evaluationquantitative/' + this.evaplan_type_id).then(res => {
                console.log(res.data)
                if(res.data.code==0){
                  this.is_edit = false;
                  //此处绑定了 页面头部的 基础信息 在数据正确返回时赋值
                  this.startTime = res.data.data.start_time;
                  this.endTime = res.data.data.end_time
                  this.company_name = res.data.data.company_name;

                  this.control_internal = res.data.data.control_internal;
                  this.evaluation_index = res.data.data.evaluation_index;
                  this.adjustment_factor = res.data.data.adjustment_factor;
                  this.evaluate_results = res.data.data.evaluate_results;
                  this.bl_year_evaluate = res.data.data.bl_year_evaluate;
                }else {
                  this.$alert(res.data.message,"提示")
                }
              }).then(() => {
                var start = formatTimeIncludeText(this.startTime + '000')
                var end = formatTimeIncludeText(this.endTime + '000')
                this.duringEvaluation = start + "-" + end
              })
            }else{
              this.$alert(res.data.message,"提示")
            }
          })
        }else if(this.bl_year_evaluate==1){
          this.evaluation_index.forEach(el=>{
            if(el.current_implementation>el.current_objectives){
              el.coinso_score = el.inin_standard_score
            }else if(el.current_implementation<el.current_objectives && el.current_implementation> el.previous_implementation){
              el.coinso_score = el.inin_standard_score*0.5
            }else if(el.current_implementation < el.previous_implementation){
              el.coinso_score = 0
            }
          })

          this.adjustment_factor.forEach(el=>{
              if(el.adpa_identify==0){
                el.coadpaso_score = 0
              }
          })

          this.axios.put('api/v1/flow/evaluationquantitative/'+this.evaplan_type_id,{
            evaplan_type_id:this.evaplan_type_id,
            evaluation_index:this.evaluation_index,
            adjustment_factor:this.adjustment_factor
          }).then(res=>{
            if(res.data.code==0){
              // 重新请求当前评价计划的数据
              this.axios.get('/api/v1/flow/evaluationquantitative/' + this.evaplan_type_id).then(res => {
                console.log(res.data)
                if(res.data.code==0){
                  this.is_edit = false;
                  //此处绑定了 页面头部的 基础信息 在数据正确返回时赋值
                  this.startTime = res.data.data.start_time;
                  this.endTime = res.data.data.end_time
                  this.company_name = res.data.data.company_name;

                  this.control_internal = res.data.data.control_internal;
                  this.evaluation_index = res.data.data.evaluation_index;
                  this.adjustment_factor = res.data.data.adjustment_factor;
                  this.evaluate_results = res.data.data.evaluate_results;
                  this.bl_year_evaluate = res.data.data.bl_year_evaluate;
                }else {
                  this.$alert(res.data.message,"提示")
                }
              }).then(() => {
                var start = formatTimeIncludeText(this.startTime + '000')
                var end = formatTimeIncludeText(this.endTime + '000')
                this.duringEvaluation = start + "-" + end
              })
            }
          })
        }

      },
      editQuantitative() {
        this.is_edit = !this.is_edit
      },
      //评价类型列表
      getQualifiedList() {
        return this.axios.get('/api/v1/flow/evaluationquantitative')
      }
    },
    computed: {},
    mounted() {
      // this.initData();
    }
  })
</script>
<style scoped>
  /*common*/
  .inner-container {
    margin-bottom: 20px;
    padding-left: 10px;
    padding-right: 10px;
    padding-bottom: 10px;
    box-sizing: border-box;
    border: .5px solid #8cc34b;
    background: #f3f9ed;
  }

  .inner-container .inner-title {
    font-size: 16px;
    color: #8cc34b;
    line-height: 3.2em;
    font-weight: 500;
  }

  /*common*/
  .evaluatuion-content {
    padding-left: 10px;
    padding-right: 10px;
    box-sizing: border-box;
  }

  .evaluatuion-container {
    min-width: 1400px;
    margin-left: auto;
    margin-right: auto;
    padding:10px 10px 64px;
    box-sizing: border-box;
  }

  .evaluatuion-container .el-table >>> .el-table__header .has-gutter th {
    text-align: center;
    background: #d3e9bb;
  }

  .evaluatuion-container .title {
    text-align: center;
    line-height: 42px;
    font-size: 14px;
    color: #666;
  }

  .evaluatuion-container .title {
    text-align: center;
    font-size: 14px;
    color: #666;
  }

  .evaluatuion-container .evaluatuion-time {
    margin-left: 64px;
  }

  .evaluatuion-results {
    margin-top: 40px;
    background: #fcd2d3;
    color: #f46c6e;
    font-size: 15px;
    text-align: center;
    padding: 36px;
    box-sizing: border-box;
  }
</style>
